package com.djq.hhc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.djq.hhc.dataprocess.ImgLoadUtils;
import com.djq.hhc.model.shop.ShopGoods;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.ShopGoodsService;
import com.djq.hhc.vo.ShopGoodsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/05/下午 5:01
 * @Description:
 */

@RestController
@RequestMapping(value = "/shop/shopGoods")
public class ShopGoodsController {

    @Autowired
    private ShopGoodsService shopGoodsService;

    //购买商品
    @PostMapping("buyGoods/{id}")
    public Result buyGoods(@RequestHeader("Authorization") String token, @PathVariable Long id) {
        return shopGoodsService.buyGoods(token, id);
    }

    //获得所有的商品
    @GetMapping("getAllGoods")
    public Result getAllGoods() {
        List<ShopGoods> list = shopGoodsService.list();
        return Result.ok(list);
    }

    //修改
    @PutMapping("update")
    public Result updateById(@RequestBody ShopGoods shopGoods) {
        return shopGoodsService.updateGoodsById(shopGoods);
    }

    //删除
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Integer id) {
        return shopGoodsService.removeGoodsById(id);
    }

    //通过id获取
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        return shopGoodsService.getGoodsById(id);
    }

    //添加
    @PostMapping("save")
    public Result save(@RequestBody ShopGoods shopGoods) {
        return shopGoodsService.saveGoods(shopGoods);
    }

    //分页
    @PostMapping("{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody(required = false) ShopGoodsVo shopGoodsVo) {
        //创建page对象，传递当前页和每页记录数
        Page<ShopGoods> pageParam = new Page<>(page, limit);
        //获取条件值
        String goodsName = shopGoodsVo.getGoodsName();
        String details = shopGoodsVo.getDetails();
        Integer num = shopGoodsVo.getNum();
        Long flowerId = shopGoodsVo.getFlowerId();
        //封装条件
        QueryWrapper<ShopGoods> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(goodsName)) {
            wrapper.like("goods_name", goodsName);
        }
        if (!StringUtils.isEmpty(details)) {
            wrapper.like("details", details);
        }
        if (!StringUtils.isEmpty(num)) {
            wrapper.eq("num", num);
        }
        if (!StringUtils.isEmpty(flowerId)) {
            wrapper.eq("flower_id", flowerId);
        }
        //调用方法得到分页查询结果
        IPage<ShopGoods> pageModel = shopGoodsService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

    /**
     * 下载图片
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        String s = ImgLoadUtils.upLoad(file);
        return Result.ok(s);
    }

    /**
     * 下载图片
     * @param name
     * @param response
     */
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response){
        ImgLoadUtils.downLoad(name, response);
    }
}
